home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / VISUALBA / PBFORMT2.ZIP / PBFORMAT.BAS
BASIC Source File  |  1994-02-08  |  15KB  |  443 lines

  1. 'PBFORMAT.BAS by Thaddy de Koning, based on:
  2. 'QBFORMAT.BAS by Cornel Huth.
  3. '14-October-1993 - Modified to format 2.8MB Floppies by Allen Hillman
  4. 'format MS-DOS floppy disks using PB and BIOS
  5. %F360 = &HFD
  6. %F1200 = &HF9
  7. 'NOTE: Original code contained mayor BUG with 3.5 inch types
  8. 'Fixed that in this PB version.
  9. '*** %F720 media byte is NEGATIVE to differ from %F1200 ***
  10. %F720 = -&HF9
  11. %F1440 = &HF0
  12. '*** %F2880 media byte is negative to differ from %F1440
  13. %F2880 = -&HF0
  14. %RETRIES = 5                            '%RETRIES on BIOS error
  15. $INCLUDE "c:\pb3\REGNAMES.INC"
  16. TYPE ADDRFIELDtype
  17.   track AS STRING * 1
  18.   head AS STRING * 1
  19.   sector AS STRING * 1
  20.   bytesec AS STRING * 1
  21. END TYPE '4
  22. TYPE INFOtype
  23.   OEM AS STRING * 8                     'system name
  24.   BS AS WORD                            'bytes/sector
  25.   SC AS BYTE                            'STRING * 1 'sectors/cluster
  26.   RS AS WORD                            'reserved sectors
  27.   NF AS BYTE                            'STRING * 1 'FATs
  28.   DE AS WORD                            'root directory entries
  29.   TS AS WORD                            'total sectors on volume
  30.   MB AS STRING * 1                      'media byte
  31.   SF AS WORD                            'sectors/FAT
  32.   ST AS WORD                            'sectors/track
  33.   NH AS WORD                            'heads
  34.   HS AS WORD                            'hidden sectors
  35. END TYPE '27
  36. TYPE BOOTRECtype
  37.   jmp AS STRING * 3
  38.   parms AS INFOtype
  39.   code AS STRING * 482
  40. END TYPE '512
  41. DEFINT A-Z
  42. 'INT 1Eh disk parameter table vectors
  43. DIM OldDPTseg AS SHARED WORD, OldDPToff AS SHARED WORD
  44. DIM NewDPTseg AS SHARED WORD, NewDPToff AS SHARED WORD
  45. 'Number of tracks on media
  46. SHARED NoTracks
  47. 'format info for media
  48. DIM Info AS SHARED INFOtype
  49. 'interface with INTERRUPTX routine
  50. 'boot record buffer
  51. DIM BootRec AS SHARED BOOTRECtype
  52. 'sector buffer to write FAT & root directory sectors
  53. DIM SectorBuff AS SHARED STRING * 512
  54. $STATIC
  55. 'Allocate address field data to max possible sectors per track
  56. DIM AddrField(1 TO 36) AS SHARED ADDRFIELDtype
  57. BootSector:
  58. DATA &HEB,&H3E,&H90,&H20,&H20,&H20,&H20,&H20,&H20,&H20,&H20,&H0,&H0,&H0,&H0,&H0
  59. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  60. DATA &HA,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  61. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  62. DATA &H2B,&HC0,&H8E,&HD0,&HBC,&H0,&H7C,&HB8,&HC0,&H7,&H8E,&HD8,&H8E,&HC0,&HBE,&H3
  63. DATA &H0,&HBF,&HAF,&H0,&HB9,&H4,&H0,&HF3,&HA5,&HBE,&HAF,&H0,&HB4,&HE,&H8A,&H4
  64. DATA &HA,&HC0,&H74,&H7,&H56,&HCD,&H10,&H5E,&H46,&HEB,&HF1,&HFC,&HBE,&H7A,&H0,&HBF
  65. DATA &H0,&H2,&HB9,&H0,&H2,&HF3,&HA4,&HE9,&H86,&H1,&HB8,&H1,&H2,&H2B,&HDB,&HB9
  66. DATA &H1,&H0,&HBA,&H50,&H0,&HCD,&H13,&H72,&HC,&HBB,&HFE,&H1,&H81,&H3F,&H55,&HAA
  67. DATA &H75,&H3,&HE9,&HE5,&HFD,&HBE,&H58,&H2,&HB4,&HE,&H8A,&H4,&HA,&HC0,&H74,&H7
  68. DATA &H56,&HCD,&H10,&H5E,&H46,&HEB,&HF1,&H2B,&HC0,&HCD,&H16,&HCD,&H19,&HCD,&H18,&H20
  69. DATA &H20,&H20,&H20,&H20,&H20,&H20,&H20,&H20,&H6E,&H6F,&H6E,&H2D,&H62,&H6F,&H6F,&H74
  70. DATA &H61,&H62,&H6C,&H65,&H20,&H64,&H69,&H73,&H6B,&H20,&H69,&H6E,&H20,&H41,&H3A,&HD
  71. DATA &HA,&H0,&H4E,&H6F,&H20,&H62,&H6F,&H6F,&H74,&H20,&H64,&H69,&H73,&H6B,&H20,&H66
  72. DATA &H6F,&H75,&H6E,&H64,&H2C,&H20,&H72,&H65,&H70,&H6C,&H61,&H63,&H65,&H20,&H61,&H6E
  73. DATA &H64,&H20,&H70,&H72,&H65,&H73,&H73,&H20,&H61,&H20,&H6B,&H65,&H79,&H20,&HD,&HA
  74. DATA &HD,&HA,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  75. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  76. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  77. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  78. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  79. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  80. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  81. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  82. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  83. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  84. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  85. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  86. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  87. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  88. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0
  89. DATA &H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H0,&H55,&HAA
  90. '============================================================================
  91. 'The following code is a sample run to format a disk
  92. 'All other code can be compiled and put in a library
  93. 'format test drive A: double-sided/36-sector media (2880K)
  94. drive = 0: media = %F1440
  95. CLS : DO: LOOP WHILE INKEY$ <> ""
  96. INPUT "Insert disk to format in drive A: and press a key", a$
  97. INPUT "Press a key to start PBFORMAT", a$
  98. PRINT
  99.  
  100. xerr = PBFORMAT(drive, media)
  101. IF xerr THEN
  102.   errl = xerr \ 256
  103.   errc = xerr AND 255
  104.   PRINT "*** Error level:"; errl; " code:"; errc
  105. ELSE
  106.   PRINT "done."
  107. END IF
  108. STOP
  109.  
  110. SUB ComputeCHS (LogSec, cyl, hd, sec)
  111. 'convert a DOS logical sector to BIOS form
  112. CylSec = Info.ST * Info.NH
  113. cyl = LogSec \ CylSec
  114. rm = LogSec - (cyl * CylSec)
  115. hd = rm \ Info.ST
  116. sec = rm - (hd * Info.ST) + 1
  117. END SUB
  118. FUNCTION FormatDisk (drive)
  119. 'format a track at a time a side at a time
  120. 'any error aborts format,diskette presumed unreliable
  121. 'retry format 1 or 2 more times before trashing the diskette
  122. y=Pos(0)
  123. x=CSRLIN
  124. FOR track = 0 TO (NoTracks - 1)
  125.   LOCATE x,y:PRINT "Formatting track:";track
  126.   'This is where you insert any printed info.
  127.   'If you don't want it, remove it......
  128.   FOR head = 0 TO (Info.NH - 1)
  129.     FOR i = 1 TO %RETRIES
  130.       xerr = FormatTrack(drive, track, head)
  131.       IF xerr = 0 THEN EXIT FOR
  132.     NEXT
  133.     IF xerr THEN FormatDisk = xerr: EXIT FUNCTION
  134.   NEXT
  135. NEXT
  136. FormatDisk = 0
  137. END FUNCTION
  138. FUNCTION FormatTrack (drive, track, head)
  139. 'Initialize address field for each sector on this track
  140. FOR sec = 1 TO Info.ST
  141.   AddrField(sec).track = CHR$(track)
  142.   AddrField(sec).head = CHR$(head)
  143.   AddrField(sec).sector = CHR$(sec)
  144.   AddrField(sec).bytesec = CHR$(2)      'bytecode 2 = 512-byte sector
  145. NEXT
  146. reg %ax, &H500 + Info.ST                'format track with sectors/track
  147. reg %cx, (track * 256) + 1              'track to format,start with sector 1
  148. Reg %dx, (head * 256) + drive           'head,drive
  149. Reg %es, VARSEG(AddrField(1))           'point to address field data
  150. Reg %bx, VARPTR(AddrField(1))
  151. CALL INTERRUPT &H13
  152. cf = Reg (%flags) AND 1                 'cf=1 if disk error
  153. IF cf THEN
  154.   E?? = Reg (%ax)
  155.   'IF e& < 0 THEN e& = e& + 65536
  156.   SHIFT RIGHT e??,8                     ' = e& \ 256 'return with status byte
  157.   Formattrack=e??
  158.   ResetFDC drive
  159. ELSE
  160.   Reg %ax, &H400 + Info.ST              'ok, verify track integrity-
  161.   CALL INTERRUPT &H13                   '-optional but recommended on format
  162.   cf = Reg (%flags) AND 1               'cf=1 if disk error
  163.   IF cf THEN
  164.     e?? = Reg (%ax)
  165.     SHIFT RIGHT e??,8                   ' = e?? \ 256 'return with status byte
  166.     Formattrack=e??
  167.     ResetFDC drive
  168.   ELSE
  169.     FormatTrack = 0                     'format ok
  170.   END IF
  171. END IF
  172. END FUNCTION
  173. SUB InitFormatParms (media)
  174. 'set up media's format data
  175. Info.OEM = "IBM  PB3"                   'avoid changing 'IBM'
  176. Info.BS = 512
  177. Info.RS = 1
  178. Info.NF = 2                             'CHR$(2)
  179. Info.NH = 2
  180. Info.HS = 0
  181. SELECT CASE media
  182. CASE %F360
  183.   Info.SC = 2                           'CHR$(2)
  184.   Info.DE = 112
  185.   Info.TS = 720
  186.   Info.MB = CHR$(%F360)
  187.   Info.SF = 2
  188.   Info.ST = 9
  189. CASE %F1200
  190.   Info.SC = 1                           'CHR$(1)
  191.   Info.DE = 224
  192.   Info.TS = 2400
  193.   Info.MB = CHR$(%F1200)
  194.   Info.SF = 7
  195.   Info.ST = 15
  196. CASE %F720
  197.   Info.SC = 2                           'CHR$(2)
  198.   Info.DE = 112
  199.   Info.TS = 1440
  200.   Info.MB = CHR$(ABS(%F720))
  201.   Info.SF = 3
  202.   Info.ST = 9
  203. CASE %F1440
  204.   Info.SC = 1                           'CHR$(1)
  205.   Info.DE = 224
  206.   Info.TS = 2880
  207.   Info.MB = CHR$(%F1440)
  208.   Info.SF = 9
  209.   Info.ST = 18
  210. case %F2880
  211.   In